import { createRouter, createWebHistory } from 'vue-router'
import ActicleChannel from '@/views/acticle/ActicleChannel.vue'
import ActicleManage from '@/views/acticle/ActicleManage.vue'
import LayoutContainer from '@/views/layout/LayoutContainer.vue'
import LoginPage from '@/views/login/LoginPage.vue'
import { useUserStore } from '@/stores'
// import UserAvatar from '@/views/user/UserAvatar.vue'
// import UserPassword from '@/views/user/UserPassword.vue'
// import UserProfile from '@/views/user/UserProfile.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: LoginPage },
    {
      path: '/',
      component: LayoutContainer,
      redirect: '/article/manage',
      children: [
        {
          path: '/article/manage',
          component: ActicleManage
        },
        {
          path: '/article/channel',
          component: ActicleChannel
        },
        {
          path: '/user/avatar',
          component: () => import('@/views/user/UserAvatar.vue')
        },
        {
          path: '/user/profile',
          component: () => import('@/views/user/UserProfile.vue')
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to) => {
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') return '/login'
})

export default router
